package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.enums.VersionEnum;
import cn.huiyunche.base.service.interfaces.VersionService;
import cn.huiyunche.base.service.mappers.SysVersionNoteMapper;
import cn.huiyunche.base.service.mappers.ext.VersionNoteViewMapper;
import cn.huiyunche.base.service.model.SysVersionNote;
import cn.huiyunche.base.service.model.SysVersionNoteExample;
import cn.huiyunche.base.service.vo.Result;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by LYN on 6/18/16.
 */
@Service

public class VersionServiceImpl implements VersionService {

    private static final Logger LOGGER = LoggerFactory.getLogger(VersionServiceImpl.class);

    @Autowired
    private VersionNoteViewMapper versionNoteViewMapper;

    @Autowired
    private SysVersionNoteMapper sysVersionNoteMapper = null;

    private SysVersionNoteMapper getSysVersionNoteMapper() {
        return this.sysVersionNoteMapper;
    }

    @Override
    public SysVersionNote getLasterVersionIOS(String platform) {
        return getVersionNote(platform, VersionEnum.IOS.getValue());
    }

    @Override
    public SysVersionNote getLasterVersionAndroid(String platform) {
        return getVersionNote(platform, VersionEnum.ANDROID.getValue());
    }

    private SysVersionNote getVersionNote(String project, Integer appType) {
        return getVersionNoteViewMapper().selectLatestVersionNote(project, appType);
    }

    public VersionNoteViewMapper getVersionNoteViewMapper() {
        return versionNoteViewMapper;
    }

    @Override
    public Result<List<SysVersionNote>> list() throws Exception {
        Result<List<SysVersionNote>> result = new Result<>(true, null, "数据加载成功");
        result.setData(this.getSysVersionNoteMapper().selectByExample(new SysVersionNoteExample()));
        return result;
    }

    @Override
    public Result<Object> modifyVersion(SysVersionNote versionNote) throws Exception {
        LOGGER.info("modifyVersion params versionNote: {}", versionNote);
        Result<Object> result = new Result<>(true, null, "数据操作成功");
        if (StringUtils.isBlank(versionNote.getAppType())) {
            LOGGER.info("appType is null");
            throw new BusinessException("APP类型不能为空");
        } else if (StringUtils.isBlank(versionNote.getPlatform())) {
            LOGGER.info("platform is null");
            throw new BusinessException("平台类型不能为空");
        } else if (StringUtils.isBlank(versionNote.getVersion())) {
            LOGGER.info("version is null");
            throw new BusinessException("版本号不能为空");
        } else if (versionNote.getVersionCode() == null) {
            LOGGER.info("versionCode is null");
            throw new BusinessException("版本发布序号不能为空");
        }
        if (null == versionNote.getId()) {
            result.setData(this.getSysVersionNoteMapper().insertSelective(versionNote));
        } else {
            result.setData(this.getSysVersionNoteMapper().updateByPrimaryKeySelective(versionNote));
        }
        return result;
    }

    @Override
    public Result<Object> deleteVersion(Long id) throws Exception {
        LOGGER.info("deleteVersion params id: {}", id);
        Result<Object> result = new Result<>(true, null, "数据删除已成功");
        if (null == id) {
            LOGGER.info("id is null");
            throw new BusinessException("版本ID不能为空");
        }
        if (0 == id) {
            LOGGER.info("id is 0");
            throw new BusinessException("版本ID不能为0");
        }

        result.setData(this.getSysVersionNoteMapper().deleteByPrimaryKey(id));
        return result;
    }

    @Override
    public SysVersionNote selectVersionId(Long id) throws Exception {
        LOGGER.info("selectVersionId params id: {}", id);
        return this.getSysVersionNoteMapper().selectByPrimaryKey(id);
    }
}
